package cn.com.joyplus.wechat.controller;

import cn.com.joyplus.wechat.service.WechatCoreApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 王灏川(wangrs@qq.com) on 2017/8/11 0011.
 */
@Controller
@RequestMapping("/")
public class IndexController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    WechatCoreApiService wechatCoreApiService;


    @RequestMapping
    public String index() {
        return "home";
    }

    @RequestMapping("public/{page}")
    public String publicPage(@PathVariable String page, HttpServletRequest req, Model model) {

        model.addAttribute("jsdkConfig",
                wechatCoreApiService.getJsdkConfig(
                        wechatCoreApiService.getJsApiTicket(wechatCoreApiService.getWechatAccessToken()),
                        req.getRequestURL().toString()
                ));
        return String.format("public/%s", page);
    }

    @RequestMapping("login")
    public String login() {
        return "login";
    }
}
